<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="900px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="110px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12" v-if="!isAdd">
          <el-form-item label="行号" prop="lineno">
            <el-input-number v-model="model.lineno" controls-position="right" :step="1" :min="0" style="width:100%" class="normal" disabled></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资源申请明细" prop="resourceapplylineid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.resourceapplylineid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmResourceApplyLineRefDialog" v-if="!disableEditSubmit"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="资源编码(原)" prop="resourcecodeold">
            <el-input v-model="model.resourcecodeold" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资源名称(原)" prop="resourcenameold">
            <el-input v-model="model.resourcenameold" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单价(原)" prop="unitpriceold">
            <el-input-number v-model="model.unitpriceold" controls-position="right" :precision="2" disabled style="width:93%"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位类型(原)">
            <el-input v-model="model.dispunittypeold" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="总金额(原)" prop="totalmoneyold">
            <el-input-number v-model="model.totalmoneyold" controls-position="right" :precision="2" disabled style="width:93%"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="申请说明(原)" prop="applyexplainold">
            <el-input v-model="model.applyexplainold" type="textarea" :rows="2" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="model.unittypeold !== '3'">
        <el-col :span="12">
          <el-form-item label="开始时间(原)" prop="starttimeold">
            <el-date-picker v-model="model.starttimeold" type="datetime" placeholder="开始时间(原)" value-format="yyyy-MM-dd HH:mm:ss" :readonly="true" class="is-disabled"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间(原)" prop="endtimeold">
            <el-date-picker v-model="model.endtimeold" type="datetime" placeholder="结束时间(原)" value-format="yyyy-MM-dd HH:mm:ss" :readonly="true" class="is-disabled"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="model.unittypeold === '3'">
        <el-col :span="12">
          <el-form-item label="数量(原)" prop="numold">
            <el-input-number v-model="model.numold" controls-position="right" class="normal" disabled style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="model.unittypeold !== '3'">
        <el-col :span="12">
          <el-form-item label="开始时间(新)" prop="starttime" :required="true" :rules="rules.validateRequire">
            <el-date-picker v-model="model.starttime" :type="model.unittypeold === '2' ? 'datetime' : 'date'" placeholder="开始时间(新)" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间(新)" prop="endtime" :required="true" :rules="rules.validateRequire">
            <el-date-picker v-model="model.endtime" :type="model.unittypeold === '2' ? 'datetime' : 'date'" placeholder="结束时间(新)" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" v-if="model.unittypeold === '3'" :required="true" :rules="rules.validateRequire">
          <el-form-item label="数量(新)" prop="num">
            <el-input-number v-model="model.num" controls-position="right" :step="1" :min="0" class="normal" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总金额(新)" prop="totalmoney" :required="true" :rules="rules.validateRequire">
            <el-input-number v-model="calcTotalMoney" controls-position="right" :precision="2" disabled style="width:93%"></el-input-number>
            <span>元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item label="申请说明(新)" prop="applyexplain" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.applyexplain" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="state">
            <el-input v-model="model.dispstate" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">
        <svg-icon icon-class="save" />保存</el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-resource-apply-line-ref ref="pmResourceApplyLineRef" @confirm="getpmResourceApplyLineRefData" :isSingleSelect="true" :projectid="projectid"></tlpm-resource-apply-line-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList'
import tlpmResourceApplyLineRef from '@/views/ProjectManage/pmResourceApplyLine/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlpmProjectRef, tlpmResourceApplyLineRef },
  data() {
    return {
      moduleTitle: '资源变更明细',
      urlPrefix: '/pm/resourceChangeLine/',
      url: {
        // 额外的url
      }
    }
  },
  props: {
    parentId: {
      type: [Number, String],
      default: ''
    },
    projectid: {
      type: [Number, String],
      default: ''
    }
  },
  created() {
    console.log()
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, 'projectid', this.projectid)
        this.$set(this.model, 'resourcechangeid', this.parentId)
      }
    },
    /** 提交前执行 */
    beforeSubmit() {
      const { starttime, endtime } = this.model
      if (starttime && endtime && starttime >= endtime) {
        this.$tap.alert('开始时间应该小于结束时间', 'warning')
        return false
      }
      return true
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'projectid', selectionRow.id)
    },
    /** 打开资源申请明细参照 */
    openpmResourceApplyLineRefDialog() {
      this.$refs.pmResourceApplyLineRef.visible = true
    },
    /** 获取资源申请明细参数数据 */
    getpmResourceApplyLineRefData(selectionRow, selectKey, selectionValue) {
      const {
        id,
        resourcecode,
        resourcename,
        unitprice,
        unittype,
        dispunittype,
        totamoney,
        applyexplain,
        starttime,
        endtime,
        num
      } = selectionRow
      this.$set(this.model, 'resourceapplylineid', id)
      this.$set(this.model, 'resourcecodeold', resourcecode)
      this.$set(this.model, 'resourcenameold', resourcename)
      this.$set(this.model, 'unitpriceold', unitprice)
      this.$set(this.model, 'unittypeold', unittype)
      this.$set(this.model, 'dispunittypeold', dispunittype)
      this.$set(this.model, 'totalmoneyold', totamoney)
      this.$set(this.model, 'applyexplainold', applyexplain)
      this.$set(this.model, 'starttimeold', starttime)
      this.$set(this.model, 'endtimeold', endtime)
      this.$set(this.model, 'numold', num)
    }
  },
  computed: {
    calcTotalMoney: {
      get() {
        const { unitpriceold: unitprice, unittypeold: unittype, starttime, endtime } = this.model
        let { num } = this.model
        let totalMoney = 0
        let calcFlag = false
        if (unittype === '1') {
          // 天
          if (starttime && endtime) {
            num = (new Date(endtime) - new Date(starttime)) / (1000 * 60 * 60 * 24) + 1
            calcFlag = true
            this.$set(this.model, 'num', null)
          }
        } else if (unittype === '2') {
          // 小时
          if (starttime && endtime) {
            num = Math.ceil((new Date(endtime) - new Date(starttime)) / (1000 * 60 * 60))
            calcFlag = true
            this.$set(this.model, 'num', null)
          }
        } else if (unittype === '3') {
          // 数量
          if (unitprice && num !== undefined) {
            calcFlag = true
            this.$set(this.model, 'starttime', null)
            this.$set(this.model, 'endtime', null)
          }
        }
        if (calcFlag) {
          totalMoney = unitprice * num
          this.$set(this.model, 'totalmoney', totalMoney)
          return totalMoney
        } else {
          return this.model.totamoney
        }
      },
      set() {}
    }
  }
}
</script>
